package com.hdl.school.utils;

public class CRCUtil {
  public String ToModbusCRC16(String s){
    int CRC = 0x0000ffff;
    for (int i=0;i<s.length();i=i+2){
      int CRCL=CRC&0x000000FF;//低八位
      int CRCH=CRC&0x0000FF00;//高八位
      String CRCIn = s.substring(i,i+2);
      int a = Integer.parseInt(CRCIn,16);//待处理数据转16进制
      CRC=CRCH+CRCL^a;
      for(int j=0;j<8;j++){
        if((CRC&0x0001)==0){
          CRC=CRC>>1;
        }else {
          CRC>>=1;
          CRC=CRC^0xA001;
        }
      }
    }
    return HexUtil.reserveHex(Integer.toHexString(CRC));
  }
}
